package com.learning.rocketmq.optimal.throughput;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;

/**
 * ClassName: OptimalConsumer
 * Description:
 * Date: 2019/1/14 17:25 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class OptimalConsumer {

    public static void main(String[] args) {

    }


    /**
     *
     * @throws MQClientException
     */
    public static void consumer() throws MQClientException {
        DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer("base_consumer_group");
        pushConsumer.setNamesrvAddr("10.0.64.106:9876;10.0.64.107:9876");
        pushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);

        pushConsumer.setConsumeThreadMin(10);
        pushConsumer.setConsumeThreadMax(50);

        pushConsumer.subscribe("tag_topic", "tag_msg");
        pushConsumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
            System.out.printf(Thread.currentThread().getName() + "收到新消息：" + msgs + "%n");
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });

        pushConsumer.start();
    }
}
